<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Brand extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	function admin_index(){
		$this->load->model('brand_model','brandModel');
		$sortBrand["order"] = 'CONVERT( brand_name USING tis620 ) ASC';
		$brands = $this->brandModel->get($sortBrand);
		$this->load->model('model_model','modelModel');
		$sortModel["order"] = 'CONVERT( mod_name USING tis620 ) ASC';
		$modelsResult = $this->modelModel->get($sortModel);
		if(!empty($modelsResult)){
			foreach ($modelsResult as $key => $value) {
				$models[$value["brand_id"]][] = $value;
			}
		}
		$this->_assign('brands',$brands);
		$this->_assign('models',$models);
		$this->_fetch('admin_index');
	}


	function admin_form(){
		$wherebrand['order'] = "id ASC";
		$all_brands = $this->brandModel->get($wherebrand);
		$this->_assign('all_cats',$all_cats);
		$this->_assign('all_brands',$all_brands);
		$this->_fetch('admin_form');
	}

	function admin_add($id = ""){
		$_post = $this->input->post();
		$this->load->model("category_model","categoryModel");
		$this->load->model("category_brand_model","categoryBrandModel");
		if(empty($_post["submit"])){
			if(!empty($id)){
				$brand	= $this->brandModel->get($id);
				$this->_assign('brand',$brand);	
				$whereCategoryBrand["where"]["brand_id"] = $id;
				$getCategoryBrandList = $this->categoryBrandModel->get($whereCategoryBrand);
				$this->_assign('categoryBrandList',$getCategoryBrandList);	
			}

			$whereCategories["where"]["parent_id"] = 0;
			$whereCategories['order'] = "id ASC";
			$categories = $this->categoryModel->get($whereCategories);

			$totalCategories = count($categories);
			$rowNeed = 4;
			$perRow = ceil($totalCategories/$rowNeed);
			for ($i=0; $i < $rowNeed; $i++) {
				for ($j=0; $j < $perRow; $j++) {
					$categoriesArray[$i][$j] = array_shift($categories);
					if(empty($categoriesArray[$i][$j])){
						unset($categoriesArray[$i][$j]);
					}
					if(!empty($id) && !empty($getCategoryBrandList) && !empty($categoriesArray[$i][$j])){
						foreach ($getCategoryBrandList as $key => $value) {
							if($categoriesArray[$i][$j]["id"] == $value["category_id"]){
								$categoriesArray[$i][$j]["check"] = true;
							}
						}
					}
				}
			}
			$this->_assign('categories',$categoriesArray);
			$this->_fetch('admin_add');
		}else{
			//Delete cache
			$this->cache->delete_folder("category");
			$this->cache->delete("menu_widget_html");

			$brandID = $this->brandModel->add($_post);	
			$whereCategoryBrand["where"]["brand_id"] = $brandID;
			$getCategoryBrandList = $this->categoryBrandModel->get($whereCategoryBrand);

			if(empty($getCategoryBrandList)){
				$categoryBrandData["brand_id"] = $brandID;
				foreach ($_post["categories"] as $key => $value) {
					$categoryBrandData["category_id"] = $value;
					$this->categoryBrandModel->add($categoryBrandData);
				}
			}else{
				//Check diffent
				if(!empty($_post["categories"])){
					foreach ($getCategoryBrandList as $key => $value) {
						if(($arrayOffset = array_search($value["category_id"], $_post["categories"])) !== FALSE){
							unset($getCategoryBrandList[$key]);
							unset($_post["categories"][$arrayOffset]);
						}
					}
					//Add new
					$categoryBrandData["brand_id"] = $brandID;
					foreach ($_post["categories"] as $key => $value) {
						$categoryBrandData["category_id"] = $value;
						$this->categoryBrandModel->add($categoryBrandData);
					}
				}
				//Delete
				foreach ($getCategoryBrandList as $key => $value) {
					$whereDeleteCategoryBrand["where"]["category_id"] = $value["category_id"];
					$whereDeleteCategoryBrand["where"]["brand_id"] = $value["brand_id"];
					$this->categoryBrandModel->delete($whereDeleteCategoryBrand);
				}

			}
			redirect('admin/brand');
		}
	}

  function admin_delete($id=""){
	 	$this->db->delete('ci_brand',array('brand_id'=>$id));

	 	//Delete cache
		$this->cache->delete_folder("category");
		$this->cache->delete("menu_widget_html");

	 	redirect('brand/admin_index');
	}


	function admin_addmodel($brand_id = NULL){
		$_post = $this->input->post();
		if(!empty($_post["submit"])){
			if(!empty($_post["name"])){
				//Delete cache
				$this->cache->delete_folder("category");
				$this->cache->delete("menu_widget_html");
				$this->load->model("model_model","modelModel");
				$_post["name"] = explode("\n", $_post["name"]);
				$modelData["brand_id"] = $_post["brand_id"];
				foreach ($_post["name"] as $key => $value) {
					$modelData["name"] = $value;
					$this->modelModel->add($modelData);
				}
			}
		}else{
			if(!empty($brand_id)){
				$where["where"]["id"] = $brand_id;
				$brand = $this->brandModel->get($where);
				$this->_assign('brand',$brand);
				$this->_fetch('admin_addmodel');
			}

		}
	}
}